﻿using BackData;
using Domain;
using Domain.BOM;
using Domain.DTOs.Quality;
using Domain.RBAC;
using Domain.工艺工序;
using Infratructure;
using MediatR;
using SqlSugar;
using Team.API.Applocation.Command.Quality;

namespace Team.API.Applocation.Hanldler.Quality
{
    public class GetQualityListHandler : IRequestHandler<GetQualityListCommand, APIPaging<QualityListDto>>
    {
        private readonly IRepository<Qualitymodel> quality;
        private readonly IRepository<Workmodel> workRep;
        private readonly IRepository<Bomtable> bomRep;
        private readonly IRepository<Makestepmodel> makeRep;
        private readonly IRepository<Usermodel> userRep;

        public GetQualityListHandler(IRepository<Qualitymodel> quality, IRepository<Workmodel> workRep, IRepository<Bomtable> bomRep, IRepository<Makestepmodel> makeRep, IRepository<Usermodel> userRep)
        {
            this.quality = quality;
            this.workRep = workRep;
            this.bomRep = bomRep;
            this.makeRep = makeRep;
            this.userRep = userRep;
        }
        public async Task<APIPaging<QualityListDto>> Handle(GetQualityListCommand request, CancellationToken cancellationToken)
        {
            // 多表连接（使用LeftJoin避免外键数据缺失导致查询为空）
            var query = quality.GetAll()
                .Where(q => q.IsDel == false)
                .LeftJoin(workRep.GetAll(), (q, w) => q.Orderid == w.Id && w.IsDel == false)
                .LeftJoin(bomRep.GetAll(), (q, w, b) => q.Itemid == b.Id && b.IsDel == false)
                .LeftJoin(makeRep.GetAll(), (q, w, b, m) => q.Processid == m.Id && m.IsDel == false)
                .LeftJoin(userRep.GetAll(), (q, w, b, m, u) => q.Accountid == u.Id && u.IsDel == false);

            // 筛选条件
            if (!string.IsNullOrEmpty(request.Quaworksubject))
            {
                query = query.Where((q, w, b, m, u) => q.Quaworksubject.Contains(request.Quaworksubject));
            }
            if (!string.IsNullOrEmpty(request.Quaworkcode))
            {
                query = query.Where((q, w, b, m, u) => q.Quaworkcode.Contains(request.Quaworkcode));
            }
            if (!string.IsNullOrEmpty(request.WorkCode))
            {
                query = query.Where((q, w, b, m, u) => w.Quaworkcode.Contains(request.WorkCode));
            }
            if (!string.IsNullOrEmpty(request.Goodsname))
            {
                query = query.Where((q, w, b, m, u) => b.Goodsname.Contains(request.Goodsname));
            }
            if (!string.IsNullOrEmpty(request.QualityUsername))
            {
                query = query.Where((q, w, b, m, u) => u.RealName.Contains(request.QualityUsername));
            }

            // Select映射
            var selectQuery = query.Select((q, w, b, m, u) => new QualityListDto
            {
                Id = q.Id,
                Quaworksubject = q.Quaworksubject,
                Quaworkcode = q.Quaworkcode,
                Quaworkname = q.Quaworkname,
                Orderid = w.Id,
                WorkCode = w.Quaworkcode,
                Completenum = q.Completenum,
                Qualifiednum = q.Qualifiednum,
                Defectivenum = q.Defectivenum,
                Itemid = b.Id,
                Goodsname = b.Goodsname,
                Status = q.Status,
                Processid = q.Processid,
                Processcount = q.Processcount,
                Reportingcount = q.Reportingcount,
                Inspectioncount = q.Inspectioncount,
                Reportingtime = q.Reportingtime,
                Accountid = u.Id,
                QualityUsername = u.RealName,
                Meeage = q.Meeage,
                Createtime = q.Createtime,
                Updatetime = q.Updatetime
            })
            .MergeTable()
            .OrderByDescending(x => x.Createtime);

            // 分页查询
            int pageIndex = request.PageIndex;
            int pageSize = request.PageSize;

            // 先获取总数
            int totalCount = selectQuery.Count();

            // 再分页
            var pageData = selectQuery
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            return new APIPaging<QualityListDto>
            {
                Totalcount = totalCount,
                Code = StateCode.OK,
                Message = "获取数据成功",
                ListResult = pageData
            };
        }
    }
}
